//
//  XCFirstViewController.m
//  TTLestening
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCFirstViewController.h"
#import <RESideMenu.h>
#import <DKNightVersion.h>
#import "UIColor+Wonderful.h"
#import "XCCollectionCellForFind.h"
#import "XCCollectionCellForMy.h"
#import "XCCollectionCellForRecommend.h"
#import "Masonry.h"


#define WIDGHT  self.view.frame.size.width
#define HEIGHT  self.view.frame.size.height

#define WIDGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface XCFirstViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) UISegmentedControl *seg;
@property (nonatomic, retain) UIButton *myButton;
@property (nonatomic, retain) UIView *whiteLine;
@property (nonatomic, retain) UICollectionView *collection;

@end

@implementation XCFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor magentaColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createHeader];
    [self createCollection];


    [self.view bringSubviewToFront:self.backview];

}

- (void)createHeader {
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor turquoise], [UIColor lightGrayColor]);
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"我的", @"推荐", @"发现"]];
    
    self.navigationItem.titleView = self.seg;
    
    
//    NSLog(@"%f", WIDGHT);
    self.seg.frame = CGRectMake(0,0, WIDGHT - 100 * WIDGHTT , 30 *HEIGHTT);
    
    self.seg.selectedSegmentIndex = 2;
    
    //去掉颜色,现在整个segment都看不见
    self.seg.tintColor = [UIColor clearColor];
    
    // 设置选中时候的 segment 的属性
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],NSForegroundColorAttributeName: [UIColor whiteColor]};
    [self.seg setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    
    // 当 segment 没有被选中的时候的属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:Wonderful_WhiteColor10};
    [self.seg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    // 为 segment 添加点击事件
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    
    // 弹出左侧抽屉的 button
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-liebiao"] style:UIBarButtonItemStyleDone target:self action:@selector(setMyAction:)];
    
    // 滑动的白线
    
    
    self.whiteLine = [[UIView alloc] initWithFrame:CGRectMake(70 * WIDGHTT, 41,(WIDGHT - 140 * WIDGHTT) / 3, 3)];
    
    [self.navigationController.navigationBar addSubview:self.whiteLine];
    self.whiteLine.backgroundColor = [UIColor whiteColor];
    
    
}

/** 点击 button弹出抽屉 */
- (void)setMyAction:(UIBarButtonItem *) button {
    
    [self.sideMenuViewController presentLeftMenuViewController];
    
}

/**点击头部 seg 跟 collection 进行连动*/

- (void)segAction:(UISegmentedControl *) seg {

    self.collection.contentOffset = CGPointMake(self.seg.selectedSegmentIndex * WIDGHT, 0);

}



/**创建 collectionView*/
- (void)createCollection {

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(WIDGHT, HEIGHT - 113);
    
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
   // flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 10);
    
   self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0,64, WIDGHT, HEIGHT - 113) collectionViewLayout:flowLayout];
    
    [self.view addSubview:self.collection];
 
    
    
//    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.width.equalTo(self.view);
//        make.top.equalTo(self.view).with.offset(64);
//        make.bottom.equalTo(self.view).with.offset(-49);
//        
//        
//    }];
    
    
    
    //self.collection.contentInset = UIEdgeInsetsMake(0, 0, 0, 10);
    self.collection.backgroundColor = [UIColor yellowColor];
    self.collection.pagingEnabled = YES;
    
    self.collection.dataSource = self;
    self.collection.delegate = self;

    self.collection.contentOffset = CGPointMake(self.seg.selectedSegmentIndex * WIDGHT, 0);
    
    [self.collection registerClass:[XCCollectionCellForFind class] forCellWithReuseIdentifier:@"poolForFind"];
    [self.collection registerClass:[XCCollectionCellForMy class] forCellWithReuseIdentifier:@"poolForMy"];
    [self.collection registerClass:[XCCollectionCellForRecommend class] forCellWithReuseIdentifier:@"poolForRecommend"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 3;

}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    
    if (indexPath.item == 0) {
        
        XCCollectionCellForMy *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForMy" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor redColor];
        
        
        return cell;
        
    } else if (indexPath.item == 1) {
    
        XCCollectionCellForRecommend *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForRecommend" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor orangeColor];
        
        
        return cell;
        
        
    
    } else {
    
        XCCollectionCellForFind *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForFind" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor blueColor];
        
        return cell;
      
    }
    
}

/** collection 偏移量与头部的白线连动 */

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    
    self.seg.selectedSegmentIndex =  scrollView.contentOffset.x / WIDGHT;
    
    CGFloat whiteX = (self.seg.selectedSegmentIndex ) * ((WIDGHT - 100 * WIDGHTT) / 3);
    
    self.whiteLine.transform = CGAffineTransformMakeTranslation(whiteX, 0);
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
